package ops.weixin.cp.api.impl;

import static ops.weixin.cp.constant.WxCpApiPathConsts.User.USER_LIST;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import ops.weixin.common.error.WxErrorException;
import ops.weixin.cp.api.WxCpService;
import ops.weixin.cp.api.WxCpUserService;
import ops.weixin.cp.bean.WxCpUser;

/**
 * 用户管理接口
 * 
 * @author SuM
 * @date: 2019年12月24日下午3:53:52
 */
public class WxCpUserServiceImpl implements WxCpUserService {
	private final WxCpService mainService;
	
	public WxCpUserServiceImpl(WxCpService mainService) {
		this.mainService = mainService;
	}

	@Override
	public List<WxCpUser> listByDepartment(Long departId, Boolean fetchChild, Integer status) throws WxErrorException {
		String params = "";
		if (fetchChild != null) {
			params += "&fetch_child=" + (fetchChild ? "1" : "0");
		}
		if (status != null) {
			params += "&status=" + status;
		} else {
			params += "&status=0";
		}

		String url = this.mainService.getWxCpConfigStorage().getApiUrl(USER_LIST + departId);
		String responseContent = this.mainService.get(url, params);

		JSONObject jsonObject = JSONObject.parseObject(responseContent);
		JSONArray jsonArray = (JSONArray) jsonObject.get("userlist");
		return (List<WxCpUser>) JSONArray.parseArray(jsonArray.toString(), WxCpUser.class);
	}
}
